﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class Development : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        DevelopmentData m_Data, m_DataX;
        public DataSet dsParamaters;
        
        #endregion
        public Development()
        {
            InitializeComponent();
            this.Name = "开发报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DevelopmentData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void InitDataBinding()
        {
            try
            { 

                this.setUI.Tables.Clear(); 
                setUI.AcceptChanges();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private void Yarn_Inventory_Click(object sender, RoutedEventArgs e)
        {
            this.Yarn_Inventory.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT CODE,GLLB,ART,VAR,SUPPLIER,BATCH,SUPPLIERBATCH,INCOMEDATE,SUM(WEI) AS WEI  "
                    + " FROM INVENTORY_YARN_REPORT_LB GROUP BY CODE,GLLB,ART,VAR,SUPPLIER,BATCH,SUPPLIERBATCH,INCOMEDATE "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "YARN_INVENTORY";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["YARN_INVENTORY"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["YARN_INVENTORY"].Columns)
                    {
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.Yarn_Inventory_TO_EXCEL("EXCEL");
                this.Yarn_Inventory.Visibility = Visibility.Visible;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        private void SalesOrder_Customer_Click(object sender, RoutedEventArgs e)
        {
            this.SalesOrder_Customer.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT KDKUNDNA,VKPART,VKPFARBE, "
                    + " TRUNC(SUM(DECODE(TRIM(VKPMEEH),'M',VKPOFFEN/0.9144,VKPOFFEN)/100)) AS QUANTITY "
                    + " FROM VKPOS,KDSTAMM WHERE VKPMANDANT = KDMANDANT AND VKPKUNDNR = KDKUNDNR AND VKPSA <> 'AJ' "
                    + " GROUP BY KDKUNDNA,VKPART,VKPFARBE HAVING SUM(DECODE(TRIM(VKPMEEH),'M',VKPOFFEN/0.9144,VKPOFFEN)/100) >10000 "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "VKPOS";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["VKPOS"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["VKPOS"].Columns)
                    {
                        if (dc.ColumnName == "KDKUNDNA")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "VKPFARBE")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        } 
                    }
                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.SalesOrder_Customer_TO_EXCEL("EXCEL");
                this.SalesOrder_Customer.Visibility = Visibility.Visible;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        private void SalesOrder_Article_Click(object sender, RoutedEventArgs e)
        {
            this.SalesOrder_Article.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT VKPART, "
                    + " TRUNC(SUM(DECODE(TRIM(VKPMEEH),'M',VKPOFFEN/0.9144,VKPOFFEN)/100)) AS QUANTITY "
                    + " FROM VKPOS WHERE VKPSA <> 'AJ' "
                    + " GROUP BY VKPART HAVING SUM(DECODE(TRIM(VKPMEEH),'M',VKPOFFEN/0.9144,VKPOFFEN)/100) >10000 "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "SalesOrder";
                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.SalesOrder_Article_TO_EXCEL("EXCEL");
                this.SalesOrder_Article.Visibility = Visibility.Visible;

            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }

        private void Sizing_Technology_Click(object sender, RoutedEventArgs e)
        {
            this.Sizing_Technology.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM SIZING_TECHNOLOGY "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "SIZING_TECHNOLOGY";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["SIZING_TECHNOLOGY"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["SIZING_TECHNOLOGY"].Columns)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[dc.ColumnName] = value;

                    }
                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.Sizing_Technology_TO_EXCEL("EXCEL");
                this.Sizing_Technology.Visibility = Visibility.Visible;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Yarn_Dyeing_Click(object sender, RoutedEventArgs e)
        {
            this.Yarn_Dyeing.Visibility = Visibility.Collapsed;

            DataSet dsResult = new DataSet();
            dsResult.Clear();
            InitDataBinding();

            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + " TRIM(B.LSNAME1) AS FACTORY,TRIM(C.GSCBEZ) AS COLOR, "
                    + " A.ABPFARBE,A.ABPSOLLMENGE5/100 AS QUANTITY,A.ABPSTARTDAT,A.ABPENDDAT,'' AS ACTENDDATE "
                    + " FROM ABPOS A,LIEFDAT B,GSCOLOR C WHERE A.ABPMANDANT = B.LSMANDANT(+) AND A.ABPLIEFNR = B.LSLIEFNR(+) "
                    + " AND A.ABPMANDANT = C.GSCMANDANT(+) AND A.ABPART = C.GSCART(+) AND A.ABPVAR = C.GSCVAR(+)  "
                    + " AND A.ABPFARBE = C.GSCFARBE(+) AND A.ABPAUFART = 'GP' "
                    //  + " AND A.ABPAUFKRS = 'DB' AND A.ABPAUFLFD = '001213' "
                    ;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";
                }

                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["ABPOS"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["ABPOS"].Columns)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[dc.ColumnName] = value;

                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    DataTable tableABPOS = setUI.Tables["ABPOS"];
                    foreach (DataRow rowABPOS in tableABPOS.Rows)
                    {
                        setUI = m_Data.GetGABEW(setUI, rowABPOS);

                    }

                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = setUI;
                m_rpt.Yarn_Dyeing_TO_EXCEL("EXCEL");
                this.Yarn_Dyeing.Visibility = Visibility.Visible;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Finishing_Order_Click(object sender, RoutedEventArgs e)
        {
            this.Finishing_Order.Visibility = Visibility.Collapsed;
            InitDataBinding();
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM FINISHING_ORDER "
                                ;
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "FINISHING_ORDER";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["FINISHING_ORDER"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["FINISHING_ORDER"].Columns)
                    {
                        if (dc.ColumnName == "ABPBESCHICH")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "COMPOSITION")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "FACTORY")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        } 
                    }
                }
                m_DataX = new DevelopmentData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                DevelopmentReport m_rpt = new DevelopmentReport(dicParamater);
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.Finishing_Order_TO_EXCEL("EXCEL");
                this.Finishing_Order.Visibility = Visibility.Visible; 
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
    }
}